package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.mapper.LoginInfoMapper;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.vo.VerifyCodeVo;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class LoginInfoServiceImpl implements ILoginInfoService {
        @Autowired
        private LoginInfoMapper loginInfoMapper;
       @Autowired
       private IUserInfoService userInfoService;
       @Autowired
       private IAccountService accountService;
       @Autowired
       private IHostingService hostingService;

        @Override
        public LoginInfo selectById(long id) {
                return loginInfoMapper.selectByPrimaryKey(id);
        }

        @Override
        public void save(String confirmPwd, String password, String username, String verifycode) {
                //各种判断,用断言工具的方法,如果没抛异常,就继续返回当前的断言工具对象
                AssertUtil.instance().isLength(username.length(), Constants.VALID_PHONE_LENGTH,"手机号码格式不对")
                        .isMobile(username,"手机号码格式不对")
                        .isNotNull(password,"密码不能为空")
                        .isLength(password.length(),Constants.MIN_LENGTH_PASSWORD,Constants.MAX_LENGTH_PASSWORD,"密码长度"
                                +Constants.MIN_LENGTH_PASSWORD+"-"+Constants.MAX_LENGTH_PASSWORD)
                        .isEquals(confirmPwd,password,"两次输入内容不一致")
                        .isLength(verifycode.length(),Constants.LENGTH_VERIFYCODE,"验证码格式错误")
                        .isFalse(checkPhone(username),"手机号已被注册");//要关联数据库的判断最后才做,提高性能


                //这里判断发送验证码的时候保存进去的手机号和最后注册的手机号是否一致
                //验证码是否一致,验证码是否过期
                VerifyCodeVo vo = UserContext.getVerifyCodeVo();

                //可能出现过期或者没有存这个VO在SEssion里,之前没有发送验证码的情况
                AssertUtil.instance().isNotNull(vo,"请重新发送验证码");
                //判断两次的手机号是否一致
                String lastPhone = vo.getPhone();
                AssertUtil.instance().isEquals(lastPhone,username,"注册手机号码和验证手机号码不一致");
                //判断验证码是否过期
                Date lastSendTime = vo.getSendTime();
                String lastVerifyCode = vo.getVerifyCode();
                AssertUtil.instance().isFalse(DateUtil.getSecondsBetween(lastSendTime,new Date())>300,
                        "验证码已过期,请重新发送").isEquals(lastVerifyCode,verifycode,"验证码不一致");
                LoginInfo loginInfo = new LoginInfo();
                loginInfo.setPassword(MD5.encode(password+username));
                loginInfo.setUsername(username);
                loginInfoMapper.insert(loginInfo);
                //保存注册信息后创建账户和基本资料
                userInfoService.init(loginInfo);
                accountService.init(loginInfo);
                //同步一个接口账户
                hostingService.createAccount(loginInfo);
                //绑定手机
                hostingService.bindPhone(loginInfo);


        }

        @Override
        public boolean checkPhone(String username) {
                return (loginInfoMapper.selectCount(username)>0);
        }

        /**
         * 登录的方法
         * @param loginInfo
         * @param
         */
        @Override
        public void login(LoginInfo loginInfo, int userType) {
                        String username = loginInfo.getUsername();
                        String password = loginInfo.getPassword();
                if(userType==LoginInfo.TYPE_WEBSITE){
                        //判断手机号和密码
                        AssertUtil.instance().isMobile(username,"非法的手机格式");
                }
                AssertUtil.instance()
                        .isNotNull(password,"密码不能为空")
                        .isLength(password.length(),Constants.MIN_LENGTH_PASSWORD,Constants.MAX_LENGTH_PASSWORD,"密码长度不正确");


                //发请求去验证数据库是否有这个账号
                LoginInfo user = loginInfoMapper.checkLogin(username,MD5.encode(password+username));

                AssertUtil.instance().isNotNull(user,"密码或者账号错误")
                        .isFalse(user.getState()!=Constants.BIDREQUEST_TYPE_NORMAL,"状态异常,请联系客服");
                //验证用户类型
                AssertUtil.instance().isFalse(user.getUserType()!=userType,"用户类型异常");
                //登录成功 就把登录对象 放在SESSION里
                //判断账户对象的状态
                UserContext.setLoginInfo(user);

        }

        @Override
        public List<LoginInfo> listAuditors() {
                return loginInfoMapper.listAuditors();
        }
}
